package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.*;
import com.offcn.service.CourseService;
import com.offcn.utils.PageTool;
import com.offcn.utils.UploadUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends BaseServlet {
    CourseService courseservice = new CourseService();
    ResultBean resultBean = null;
    //获取数据及分页的方法
    private void findAllCourse(HttpServletRequest request , HttpServletResponse response) throws IOException {
        String search = request.getParameter("search");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
       //获取总数据量totalCount的方法
        int  totalCount = courseservice.findCourseCount(search);
        PageTool pageTool =new PageTool(totalCount,currentPage,Integer.parseInt(pageSize));
        //获取总数据的方法
        List<Course> courses = courseservice.findAllCourse(pageTool,search);
        pageTool.setList(courses);
        if (courses != null){
            resultBean = new ResultBean(200,"获取成功",pageTool);
        }else {
            resultBean = new ResultBean(500,"获取用户失败",pageTool);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);

    }
    //将页面上传的文件上传到服务器中
    private void uploadFile(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
        Part part = request.getPart("file");
        String filename= UploadUtils.upload(part);
        //将文件名返回到页面，在页面上显示对应的文件
        if (filename.contains("jpg") || filename.contains("png") || filename.contains("jpeg") || filename.contains("gif")){
            resultBean = new ResultBean( 1,null,filename);
        }else {
            resultBean = new ResultBean( 2,null,filename);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    private void deleteFile(HttpServletRequest request , HttpServletResponse response) {
        String filename = request.getParameter("filename");
        File file = new File("D:/eduSource/"+filename);
        file.delete();


    }
    //添加书籍的方法
    private void addCourse(HttpServletRequest request , HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course,parameterMap);
        boolean isCoures = courseservice.addCourse(course);
        if (isCoures){
            resultBean = new ResultBean(200,"添加成功",isCoures);
        }else {
            resultBean = new ResultBean(500,"添加用户失败",isCoures);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //批量删除
    private void deleteAllCourse(HttpServletRequest request , HttpServletResponse response) throws IOException {
        String deleteCourseUid = request.getParameter("isd");
        DeleteCourse.deleteCourse(deleteCourseUid);
        boolean isSuccess = courseservice.deleteAllCourse(deleteCourseUid);
        if (isSuccess){
            resultBean = new ResultBean(200,"删除成功", true);
        }else {
            resultBean = new ResultBean(500,"删除失败", false);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);

    }
    //修改书籍信息
    private void updateCourse(HttpServletRequest request , HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        String oldCid = request.getParameter("Oldcid");
        DeleteCourse.deleteCourse(oldCid);
        Map<String, String[]> parameterMap = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course,parameterMap);
       boolean isSuccess =  courseservice.updateCourse(course);
        if (isSuccess){
            resultBean = new ResultBean(200,"删除成功", true);
        }else {
            resultBean = new ResultBean(500,"删除失败", false);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
}
